package site.sorghum.ddl.compare;

import site.sorghum.ddl.DDLDialect;
import site.sorghum.ddl.entity.DdlColumnWrap;
import site.sorghum.ddl.entity.DdlIndexWrap;
import site.sorghum.ddl.entity.DdlTableWrap;

import java.util.List;
import java.util.stream.Collectors;

public class TableCompare {
    public static List<String> compareAddColumn(
            DDLDialect dialect,
            DdlTableWrap javaTableWrap,
            DdlTableWrap dbTableWrap) {

        List<DdlColumnWrap> javaColumns = javaTableWrap.getColumns();
        List<DdlColumnWrap> dbColumns = dbTableWrap.getColumns();
        List<DdlColumnWrap> compare = ColumnCompare.compare(javaColumns, dbColumns);
        return compare.stream().map(dialect::generateAddColumnDDL).collect(Collectors.toList());
    }

    public static List<String> compareAddIndex(
            DDLDialect dialect,
            DdlTableWrap javaTableWrap,
            DdlTableWrap dbTableWrap) {
        List<DdlIndexWrap> javaIndexes = javaTableWrap.getIndexes();
        List<DdlIndexWrap> dbIndexes = dbTableWrap.getIndexes();
        List<DdlIndexWrap> compareIndexes = IndexCompare.compare(javaIndexes, dbIndexes);
        return compareIndexes.stream().map(dialect::generateAddIndexDDL).collect(Collectors.toList());
    }

    public static List<String> compareDropColumn(
            DDLDialect dialect,
            DdlTableWrap javaTableWrap,
            DdlTableWrap dbTableWrap) {
        List<DdlColumnWrap> javaColumns = javaTableWrap.getColumns();
        List<DdlColumnWrap> dbColumns = dbTableWrap.getColumns();
        List<DdlColumnWrap> compare = ColumnCompare.compare(dbColumns, javaColumns);
        return compare.stream().map(it -> dialect.generateDropColumnDDL(it.getTable(), it.getName())).collect(Collectors.toList());
    }

    public static List<String> compareDropIndex(
            DDLDialect dialect,
            DdlTableWrap javaTableWrap,
            DdlTableWrap dbTableWrap) {
        List<DdlIndexWrap> javaIndexes = javaTableWrap.getIndexes();
        List<DdlIndexWrap> dbIndexes = dbTableWrap.getIndexes();
        List<DdlIndexWrap> compareIndexes = IndexCompare.compare(dbIndexes, javaIndexes);
        return compareIndexes.stream().map(it -> dialect.generateDropIndexDDL(it.getTable(), it.getIndexName())).collect(Collectors.toList());
    }
}
